Passed
Pull Request — master (#112)
by Mathieu
01:43
created

HolidayToEventsConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 29
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A convert 0 25 4
1
import {Inject} from '@nestjs/common';
2
import {
3
  Holiday,
4
  HolidayLeaveType
5
} from '../../HumanResource/Holiday/Holiday.entity';
6
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity';
7
import {IEventRepository} from '../Repository/IEventRepository';
8
import {IDateUtils} from 'src/Application/IDateUtils';
9
10
export class HolidayToEventsConverter {
11
  constructor(
12
    @Inject('IEventRepository')
13
    private readonly eventRepository: IEventRepository,
14
    @Inject('IDateUtils')
15
    private readonly dateUtils: IDateUtils
16
  ) {}
17
18
  public convert(holiday: Holiday): void {
19
    const user = holiday.getUser();
20
    const type =
21
      holiday.getLeaveType() === HolidayLeaveType.MEDICAL
22
        ? EventType.MEDICAL_LEAVE
23
        : EventType.HOLIDAY;
24
25
    const dates = this.dateUtils.getWorkedDaysDuringAPeriod(
26
      new Date(holiday.getStartDate()),
27
      new Date(holiday.getEndDate())
28
    );
29
30
    const firstDate = dates[0].toISOString();
31
    const lastDate = dates[dates.length - 1].toISOString();
32
33
    for (const date of dates) {
34
      const currentDate = date.toISOString();
35
      const time =
36
        (firstDate === currentDate && false === holiday.isStartsAllDay()) ||
37
        (lastDate === currentDate && false === holiday.isEndsAllDay())
38
          ? 50
39
          : 100;
40
41
      this.eventRepository.save(new Event(type, user, time, currentDate));
42
    }
43
  }
44
}
45